﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using BIC_GDI;



namespace MySpy1
{
    public partial class Form1 : Form
    {
        public class COMRECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
            public COMRECT()
            {
            }
            public COMRECT(int left, int top, int right, int bottom)
            {
                this.left = left;
                this.top = top;
                this.right = right;
                this.bottom = bottom;
            }
        }

        [DllImport("user32.dll", EntryPoint = "WindowFromPoint")]//指定坐标处窗体句柄       
        public static extern IntPtr WindowFromPoint(int xPoint, int yPoint);

        [DllImport("user32.dll")]
        public extern static int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

        [DllImport("user32.dll")]
        private static extern int GetWindowRect(IntPtr hwnd, ref  Rectangle lpRect);

        [DllImport("User32.dll")]
        private static extern IntPtr GetWindowDC(IntPtr hwnd);

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern bool RedrawWindow(IntPtr hwnd, COMRECT rcUpdate, IntPtr hrgnUpdate, int flags);


        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern bool UpdateWindow(IntPtr hWnd);


        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern bool InvalidateRect(IntPtr hWnd, COMRECT rect, bool erase);






        private IntPtr OldWnd;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = !timer1.Enabled;
            if (timer1.Enabled)
            {
                button1.Text = "Stop";
            }
            else
            {
                button1.Text = "Start";
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            IntPtr hWnd = WindowFromPoint(Cursor.Position.X, Cursor.Position.Y);
            // IntPtr hWnd =Win32.WindowFromPoint(Cursor.Position);

            if (OldWnd != IntPtr.Zero && OldWnd != hWnd)
            {
                Refresh(OldWnd); //erase old window
            }
            if (hWnd == IntPtr.Zero)
            {
                textBox1.Text = null;
                textBox2.Text = null;
                textBox3.Text = null;
                textBox4.Text = null;
                textBox5.Text = null;
            }
            else
            {
                OldWnd = hWnd;
                textBox1.Text = hWnd.ToString();

                textBox2.Text = GetWindowText(hWnd);
                textBox3.Text = GetClassName(hWnd);


                Rectangle rc = new Rectangle();
                GetWindowRect(hWnd, ref rc);
                
                textBox4.Text = string.Format("{0} x {1}", rc.Right - rc.Left, rc.Bottom - rc.Top);
                textBox5.Text = string.Format("({0},{1})  - ({2},{3})", rc.Left, rc.Top, rc.Right, rc.Bottom);
                Highlight(hWnd);
            }
        }

        private string GetWindowText(IntPtr hWnd)
        {
            StringBuilder sb = new StringBuilder(256);
            GetWindowText(hWnd, sb, 256);
            return sb.ToString();
        }

        private string GetClassName(IntPtr hWnd)
        {
            StringBuilder sb = new StringBuilder(256);
            GetClassName(hWnd, sb, 256);
            return sb.ToString();
        }
        public void Highlight(IntPtr hWnd)
        {
            const float penWidth = 3;
            Win32.Rect rc = new Win32.Rect();
            Win32.GetWindowRect(hWnd, ref rc);

            IntPtr hDC = Win32.GetWindowDC(hWnd);
            if (hDC != IntPtr.Zero)
            {
                using (Pen pen = new Pen(Color.Red, penWidth))
                {
                    using (Graphics g = Graphics.FromHdc(hDC))
                    {
                        Font font = new Font("Courer New", 9, FontStyle.Bold);
                        g.DrawRectangle(pen, 0, 0, rc.right - rc.left - (int)penWidth, rc.bottom - rc.top - (int)penWidth);
                        g.DrawString("BIC Tech <SPY>", font, Brushes.Red, 5, 5);
                    }
                }
            }
            Win32.ReleaseDC(hWnd, hDC);
        }
        public void Highlight2(IntPtr hWnd)
        {
            const float penWidth = 3;
            Rectangle rc = new Rectangle();
            GetWindowRect(hWnd, ref rc);

            IntPtr hDC = GetWindowDC(hWnd);
            if (hDC != IntPtr.Zero)
            {
                using (Pen pen = new Pen(Color.Red, penWidth))
                {
                    using (Graphics g = Graphics.FromHdc(hDC))
                    {
                        Font font = new Font("Courer New", 9, FontStyle.Bold);
                        g.DrawRectangle(pen, 0, 0, rc.Right - rc.Left - (int)penWidth, rc.Bottom - rc.Top - (int)penWidth);
                        g.DrawString("BIC Tech <SPY>", font, Brushes.Red, 5, 5);
                    }
                }
            }
            Win32.ReleaseDC(hWnd, hDC);
        }
        public void Refresh(IntPtr hWnd)
        {
            Win32.InvalidateRect(hWnd, IntPtr.Zero, 1 /* TRUE */);
            Win32.UpdateWindow(hWnd);
            Win32.RedrawWindow(hWnd, IntPtr.Zero, IntPtr.Zero, Win32.RDW_FRAME | Win32.RDW_INVALIDATE | Win32.RDW_UPDATENOW | Win32.RDW_ALLCHILDREN);
        }
        public void Refresh2(IntPtr hWnd)
        {
            InvalidateRect(hWnd,new COMRECT(0,0,0,0),true);
            UpdateWindow(hWnd);
            RedrawWindow(hWnd, new COMRECT(0, 0, 0, 0), IntPtr.Zero, Win32.RDW_FRAME | Win32.RDW_INVALIDATE | Win32.RDW_UPDATENOW | Win32.RDW_ALLCHILDREN);
        }
    }
}
